﻿using System;
using System.Collections;
using System.Collections.Generic;
using Xin478.CSWork.Common;

namespace Xin478.CSWork.Client
{
    public class TipList 
    {
        public static readonly TipList Instance=new TipList();
        List<ITipable> list = new List<ITipable>();
        public event EventHandler ItemAdded;
        public event EventHandler ItemRemoved;        
        
        public void Add(ITipable value)
        {
            if (value != null && FindIndex(value) == -1)
            {
                list.Add(value);
                if (ItemAdded != null) ItemAdded(value, new EventArgs());
            }
        }

        public void Remove(ITipable obj)
        {
            int index = FindIndex(obj);
            if (index != -1)
            {
                list.RemoveAt(index);
                if (ItemRemoved != null) ItemRemoved(obj, new EventArgs());
            }
        }

        public void Remove(string key)
        {
            int index = FindIndex(key);
            if (index != -1)
            {
                ITipable obj = list[index];
                list.RemoveAt(index);
                if (ItemRemoved != null && obj != null) ItemRemoved(obj, new EventArgs());
            }
        }

        public void RemoveAt(int index)
        {
            if (index >= list.Count) return;

            ITipable obj = list[index];
            list.RemoveAt(index);
            if (ItemRemoved != null && obj != null)
                ItemRemoved(obj, new EventArgs());
        }

        public void Clear()
        {
            for (int i = 0; i < list.Count; i++)
            {
                this.RemoveAt(i);
            }
        }

        public int Count { get { return list.Count; } }

        public ITipable this[int index] { get { return list[index]; } }

        int FindIndex(ITipable obj)
        {
            return FindIndex( obj.Key);
        }
        int FindIndex(string key)
        {
            for (int i = 0; i < this.Count; i++)
            {
                if (string.Compare(this.list[i].Key, key) == 0) return i;
            }
            return -1;
        }
    }

    public interface ITipable
    {
        string Title { get; }
        string Key { get; }
        void ItemClick(ITipable iTip);
    }

    public class RemindTipItem : ITipable
    {
        RemindInfo ri;
        SendInvoke sendInvoker;

        public RemindTipItem(RemindInfo ri, SendInvoke sendInvoker)
        {
            this.ri = ri;
            this.sendInvoker = sendInvoker;
        }
        public string Title { get { return ri.Detail; } }
        public string Key { get { return "R" + ri.Id; } }                                                                                                                                                                                                       
        public void ItemClick(ITipable iTip)
        {
            RemindTipForm remindTipForm = new RemindTipForm(ri);
            remindTipForm.SureButtonClick += new EventHandler(remindTipForm_SureButtonClick);
            remindTipForm.Show();
        }

        void remindTipForm_SureButtonClick(object sender, EventArgs e)
        {
            if(this.sendInvoker!=null)
                this.sendInvoker("Remind.确认提醒", ri.Id);
            RemindHelper.Instance.Remove(ri.Id);
        }
    }

}
